package edu.spec.wechat.kit;

/**
 * Created by MadFrog on 2016/10/30.
 */

import edu.spec.student.model.Student;
import edu.spec.student.service.iface.IStudentEventService;
import edu.spec.student.service.iface.IStudentLessonService;
import edu.spec.student.service.iface.IStudentService;
import edu.spec.basic.web.BeanFactoryContext;
import org.madgecko.basic.kit.BasicKit;

import java.util.Map;

/**
 * 事件消息处理工具
 */
public class EventMessageKit {
    /**
     *  事件消息包括以下：
     * 1 关注/取消关注事件
     * 2 扫描带参数二维码事件
     * 3 上报地理位置事件
     * 4 自定义菜单事件
     * 5 点击菜单拉取消息时的事件推送
     * 6 点击菜单跳转链接时的事件推送
     */

    /**
     * 处理事件消息
     * @return
     */
    public static String handleMessage(Map<String, Object> map){
        String replayContent = "";
        //消息是“上报地理位置”
        String event = (String)map.get("Event");
        if (BasicKit.isEmpty(event)){
            return replayContent;
        }
        if ("LOCATION".equals(event)){
            // 上报地理位置事件
            replayContent = handleLocationEvent(map);
        }else if("subscribe".equals(event)){
            // 用户关注公众号事件
            replayContent = handleSubscribe(map);
        }else if ("unsubscribe".equals(event)){
            // 用户取消关注公众号事件
            replayContent = handleUnsubscribe(map);
        }else if("scancode_waitmsg".equals(event)){
            // 菜单“扫描事件”
            Map infoMap = (Map)map.get("ScanCodeInfo");
            String scanResult = (String)infoMap.get("ScanResult");
            String[] strs = scanResult.split("/");
            if("studentLesson".equals(strs[strs.length-4])){
                String openId = (String)map.get("FromUserName");
                IStudentService studentService = (IStudentService) BeanFactoryContext.getService("studentService");
                Student student = studentService.loadByOpenId(openId);
                Integer studentId = student.getId();
                Integer lessonId =  Integer.parseInt(strs[strs.length-1]);
                IStudentLessonService slService = (IStudentLessonService) BeanFactoryContext.getService("studentLessonService");
                replayContent = (String)slService.signIn(studentId, lessonId).get("message");
            }
            if("".equals(strs[strs.length-4])){
                Integer eventId =  Integer.parseInt(scanResult.substring(50));
                String openId = (String)map.get("FromUserName");
                IStudentEventService seService = (IStudentEventService) BeanFactoryContext.getService("studentEventService");
                replayContent = (String)seService.signIn(openId, eventId).get("msg");
            }

        }
        return replayContent;
    }

    /**
     * 处理用户"关注"微信公众号事件
     * @return
     */
    private static String handleSubscribe(Map<String, Object> map){
        //获取openId
        //检查该openId是否绑定学号
        //      未绑定：提醒用户立即绑定学号，一经绑定不能修改，使用链接跳转
        //      已绑定：提醒用户该微信号之前已经绑定过某某学号，原来数据还在
        String replay = null;
        String bindingUrl = "http://madfrog.51vip.biz/sms/student/index";
        String openId = (String)map.get("FromUserName");
        IStudentService studentService = (IStudentService) BeanFactoryContext.getService("studentService");
        Student student = studentService.loadByOpenId(openId);
        if (BasicKit.isEmpty(student)){
            replay = "<a href=\"" + bindingUrl + "\">请立即完成学号绑定学号。</a>";

        }else {
            replay = "你好，请选择下方菜单";
        }
        return replay;
    }

    /**
     * 处理用户"取消关注"微信公众号事件
     * @return
     */
    private static String handleUnsubscribe(Map<String, Object> map){
        String replay = null;
        String openId = (String)map.get("FromUserName");
        IStudentService studentService = (IStudentService) BeanFactoryContext.getService("studentService");
        Student student = studentService.loadByOpenId(openId);
        if(!BasicKit.isEmpty(student)){
//            student.setBindStatus(BindStatus.UNBIND);
//            studentService.update(student);
        }
        return replay;
    }

    /**
     * 处理用户"上报地理位置事件"
     * @param map
     * @return
     */
    private static String handleLocationEvent(Map<String, Object> map){
//        String replayContent = "";
//        String openId = (String)map.get("openId");
//        //学生签到
//        //检查是不是重复签到，通过最后签到日期来判断
//        ISignInSumService signInSumService = (ISignInSumService) BeanFactoryContext.getService("signInSumService");
//        Date lastSignInDate = signInSumService.loadByOpenId(openId).getLastSignInDate();
//        Date currentDate = new Date();
//        if(currentDate.equals(lastSignInDate)){
//            replayContent =  "您今日已经签到，每日签到一次即可，请勿重复签到。";
//        }
//        //保存签到记录SignIn
//        ISignInService signInService = (ISignInService) BeanFactoryContext.getService("signInService");
//        signInService.signIn(map);
//        //TODO 更新SignInSum
//        replayContent  =  "今日成功签到，明天继续努力哦。";
        return "";
    }

}
